package com.nondo.rdp.finance.service.impl.analysis;

import com.nondo.rdp.auth.domain.Role;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.finance.domain.analysis.Analysis;
import com.nondo.rdp.finance.repository.analysis.AnalysisRepository;
import com.nondo.rdp.finance.service.analysis.AnalysisService;

import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.pqc.math.linearalgebra.IntUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Transactional
@Service
public class AnalysisServiceImpl implements AnalysisService {
    @Autowired
    AnalysisRepository analysisRepository;

    @Override
    public Analysis findOne(Long id) {
        return this.analysisRepository.findOne(id);
    }

    @Override
    public Analysis save(Analysis analysis) {
        return this.analysisRepository.save(analysis);
    }

    @Override
    public void update(Analysis analysis) {
        this.analysisRepository.save(analysis);
    }

    @Override
    public void delete(Long id) {
        this.analysisRepository.deleteById(id);
    }

    @Override
    public Page<Analysis> findAll(Page<Analysis> page, Analysis analysis) {
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        PageRequest pageRequest = new PageRequest(page.getPageNumber(), page.getPageSize(), sort);
        org.springframework.data.domain.Page<Analysis> dictPage = analysisRepository.findAll((root, query, builder) -> {

            List<Predicate> predicates = new ArrayList<>();

            //搜索标题
            if (StringUtils.isNotEmpty(analysis.getTitle())) {
                predicates.add(builder.like(root.get("title"), "%" + analysis.getTitle() + "%"));
            }
            //搜索类型
            if (analysis.getType() != 0 && analysis.getType() != 10000) {
                predicates.add(builder.equal(root.get("type"), analysis.getType()));
            }
            //搜索排除4的特殊类型
            if (analysis.getType() == 10000) {
                predicates.add(builder.notEqual(root.get("type"), 4));
            }

            //独立的多ID类型搜索
            if (analysis.getSearchTypeId() != null && analysis.getSearchTypeId().length() >= 1) {
                List typeList = Arrays.asList(analysis.getSearchTypeId().split(","));
                List newList = new ArrayList();
                for (Object item : typeList) {
                    newList.add(Integer.parseInt(item.toString()));
                }
                predicates.add(root.get("type").in(newList));
            }

            //搜索ID
            if (analysis.getId() != null && analysis.getId() != 0) {
                predicates.add(builder.equal(root.get("id"), analysis.getId()));
            }
            predicates.add(builder.equal(root.get("delFlag"), Role.DEL_FLAG_NORMAL));
            Predicate[] predicatesArray = new Predicate[predicates.size()];
            predicates.toArray(predicatesArray);
            return builder.and(predicatesArray);

        }, pageRequest);

        return new Page<Analysis>(dictPage);
    }

    @Override
    public Analysis findByWsId(Long wsId) {
        return analysisRepository.findByWsId(wsId);
    }
}